<template>
  <el-dialog v-model="visible" :title="!dataForm.tray ? '新增' : '修改' " :close-on-click-modal='false'
             :close-on-press-escape='false' :fullscreen='true'>
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter.native="dataFormSubmitHandle()"
             label-width="120px">

                <el-form-item label="批号" prop="lot">
            <el-input v-model="dataForm.lot" placeholder="批号"></el-input>
          </el-form-item>
          <el-form-item label="物料编码" prop="sku">
            <el-input v-model="dataForm.sku" placeholder="物料编码"></el-input>
          </el-form-item>
          <el-form-item label="库位编码" prop="location">
            <el-input v-model="dataForm.location" placeholder="库位编码"></el-input>
          </el-form-item>
          <el-form-item label="箱" prop="tray">
            <el-input v-model="dataForm.tray" placeholder="箱"></el-input>
          </el-form-item>
          <el-form-item label="格" prop="box">
            <el-input v-model="dataForm.box" placeholder="格"></el-input>
          </el-form-item>
          <el-form-item label="数量" prop="qty">
            <el-input v-model="dataForm.qty" placeholder="数量"></el-input>
          </el-form-item>
          <el-form-item label="分配量" prop="pickqty">
            <el-input v-model="dataForm.pickqty" placeholder="分配量"></el-input>
          </el-form-item>
          <el-form-item label="待上架量" prop="putqty">
            <el-input v-model="dataForm.putqty" placeholder="待上架量"></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-input v-model="dataForm.status" placeholder="状态"></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
          </el-form-item>
          <el-form-item label="更新人" prop="updatuser">
            <el-input v-model="dataForm.updatuser" placeholder="更新人"></el-input>
          </el-form-item>
          <el-form-item label="更新时间" prop="updatetime">
            <el-input v-model="dataForm.updatetime" placeholder="更新时间"></el-input>
          </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import {ref, reactive, onMounted, nextTick, computed} from 'vue';
import {getInventoryById,putInventory,postInventory} from "@/network/lswms/stock/Inventory"
import {ElMessage} from 'element-plus'
import _ from 'lodash'


export default {
  name: "inventoryAddOrUpdate",
  emits: ["refreshDataList"],
  setup(props, {emit}) {
    const visible = ref(false);
    const dataForm = ref({
                          id: '',
          lot: '',
          sku: '',
          location: '',
          tray: '',
          box: '',
          qty: '',
          pickqty: '',
          putqty: '',
          status: '',
          remark: '',
          adduser: '',
          addtime: '',
          updatuser: '',
          updatetime: ''
    });


    const dataRule = reactive({
                lot: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          sku: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          location: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          tray: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          box: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          qty: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          pickqty: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          putqty: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          status: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          remark: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
                                  // updater: [
      //       { required: true, message: '必填项不能为空', trigger: 'blur' }
      //     ],          updateDate: [
      //       { required: true, message: '必填项不能为空', trigger: 'blur' }
      //     ]   ,
                  })

    const dataFormRef = ref(null)

    const init = () => {
      visible.value = true;
      nextTick(function () {
        dataFormRef.value.resetFields();
        if (dataForm.value.id) {
                getinventoryInfo();
                }

      })
    }

        const getinventoryInfo =()=>{
                getInventoryById(dataForm.value.id).then(function (res) {
                  if (res.code !== 0) {
                    return ElMessage.error(res.msg);
                  }
                  dataForm.value = _.assignIn({}, dataForm.value, res.data);
                }).catch((err) =>{
                });
        }

    // 表单提交
    const dataFormSubmitHandle = _.debounce(() => {
      dataFormRef.value.validate(function (valid) {
        if (!valid) {
          return false;
        }

        let qing = ()=> {
          return !dataForm.value.id ?  postInventory(dataForm.value) : putInventory(dataForm.value)
        }
        qing().then(function (res) {
          if (res.code !== 0) {
            return ElMessage.error(res.msg);
          }
          ElMessage({
            message: '操作成功',
            type: 'success',
            duration: 500,
            onClose: function () {
              visible.value = false
              emit('refreshDataList')
            }
          })
        }).catch(function () {
        });

      })
    }, 1000, {'leading': true, 'trailing': false})

    return {
      visible,
      dataForm,
      dataRule,
      init,
      dataFormSubmitHandle,
      dataFormRef

    }
  },


}
</script>

<style scoped>

</style>
